import React from 'react';
import {Form, Row, Col, Button, InputNumber, message ,Radio,Input,Checkbox,Upload} from 'antd';
import { withRouter } from 'react-router-dom'
import {injectIntl} from 'react-intl';
import axios from 'axios';
import SCSelect from '../../components/pickCity/SCSelect';
import { sessionManager } from '../../utils/sessionStorage';


const FormItem = Form.Item;
const createForm = Form.create;
const RadioGroup = Radio.Group;

@withRouter
class AddCompany extends React.Component {
    constructor(props) {
        super(props);
        if(this.props.location.state===undefined){
            this.props.history.push('/company/newCompanyManage')
        }
        this.common = this.props.intl.messages.common;
        this.state = {
            type:"0",
            companyInfo:{},
            fileList:[],
        }

    }

    validatePhone = (rule, value, callback) => {
        let myreg = /^1[3|4|5|7|8|9][0-9]{9}$/;
        if (!this.props.location.state&&(value && !myreg.test(value))) {
            callback('请输入正确的手机号');
        } else {
            callback();
        }
    };

    validateOrg=(rule, value, callback) => {
        let myreg = /^[A-Z0-9]{9,9}$/;
        if (value && !myreg.test(value)) {
            callback('请输入正确的组织机构代码');
        } else {
            callback();
        }
    };

    validateOrgID=(rule, value, callback) => {
        let myreg = /^[A-Za-z0-9]+$/;
        if (value && !myreg.test(value)) {
            callback('请输入正确的企业编码');
        } else {
            callback();
        }
    };

    validateID=(rule, value, callback) => {
        let myreg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        if (value && !myreg.test(value)) {
            callback('请输入正确的身份证号');
        } else {
            callback();
        }
    };

    componentDidMount(){
        //修改入口
        if(this.props.location.state){
            axios.get('/api/corp/get',{params:{corpId:sessionManager.get('companyInfo').id}}).then(res=>{
                if(res&&res.data){
                    this.setState({
                        type:`${res.data.type}`,
                        companyInfo:res.data
                    })
                }
            })
            axios.get('/api/corp/att/qry',{params:{companyId:sessionManager.get('companyInfo').id}}).then(res=>{
                if(res&&res.data){
                    let arr=res.data.map(k=>({
                        uid:k.id,
                        id:k.id,
                        name:k.fileName,
                        status:'done',
                        url:k.file
                    }))
                    this.setState({
                        fileList:arr
                    })
                }
            })
        }
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((errors, values) => {
            if (!!errors) {
                console.log('Errors in form!!!');
                return;
            }
            console.log(values);

            if(this.state.flag){
                if(!values.card){
                    values.card=false
                }
                if(!values.sendSms){
                    values.sendSms=false
                }
                values.billUserId=this.state.companyInfo.billUserId;
                delete values.fileName;
                axios.post(`${this.props.location.state?'/api/subCompany/corp/mod':'/api/subCompany/corp/add'}`,values).then(res=>{
                    if(res&&res.data){
                        if(this.props.location.state){
                            message.success('修改企业成功')
                        }
                        if(!this.props.location.state){
                            message.success('添加企业成功')
                        }
                        this.props.history.push('/company/CompanyManage');
                    }
                })
            }      
        });
    }

    normFile = (e) => {
        if (Array.isArray(e)) {
          console.log('Upload event:', e);
          return e;
        }
        return e && e.fileList;
      }

    typeNum = (e) => {
        console.log('radio checked', e.target.value);
        this.setState({
            type: e.target.value,
        },()=>{
           this.props.form.validateFields(['idNo'], { force: true }); 
        });
      }

    render() {
        const formItemLayout = {
            labelCol: {
              xxl: { span: 8 },
              xl: { span: 7 },
            },
            wrapperCol: {
              xxl: { span: 8 },
              xl: { span: 10 },
            },
          };
          const tailFormItemLayout = {
            wrapperCol: {
              xl: {
                span: 10,
                offset: 7,
              },
              xxl: { 
                offset: 8,
                span: 8 
            },
              
            },
          };
        const {getFieldDecorator} = this.props.form;
        return (
            <div>
                <form form={this.props.form}>
                    <FormItem
                    {...formItemLayout}
                    label={"负责人账号"}
                    >
                        {getFieldDecorator('account',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                }, {
                                    validator: this.validatePhone
                                }
                            ],initialValue:this.state.companyInfo.account
                        })(
                            <Input disabled={this.state.flag||this.props.location.state}/>
                        )}
                    </FormItem>
                    <FormItem
                     {...formItemLayout}
                     label={"身份证号"}
                    >
                        {getFieldDecorator('idNo',{
                                    rules: [
                                        {
                                            required: this.state.type==="0",
                                            message: '该项为必填项'
                                        }, {
                                            validator: this.validateID
                                        }
                                    ],initialValue:this.state.companyInfo.idNo
                                })(
                            <Input disabled={this.state.flag}/>
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label="账号归属"
                    >
                    {getFieldDecorator('type',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.type})(
                        <RadioGroup disabled={this.state.flag} onChange={this.typeNum}>
                        <Radio value="0">个人</Radio>
                        <Radio value="1">企业</Radio>
                        </RadioGroup>
                    )}
                    </FormItem>
                    {this.state.type==="1"&&<FormItem
                     {...formItemLayout}
                     label={"组织机构代码"}
                    >
                        {getFieldDecorator('orgCode',{
                            rules: [
                                {
                                    required:true,
                                    message: '该项为必填项'
                                },{
                                    validator: this.validateOrg
                                }
                            ],initialValue:this.state.companyInfo.orgCode
                        })(
                            <Input disabled={this.state.flag} placeholder={'9位大写英文字母或数字'}/>
                        )}
                    </FormItem>}
                    {/* 详情========== */}
                    <FormItem
                    {...formItemLayout}
                    label={"企业编码"}
                    >
                        {getFieldDecorator('id',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                },
                                {
                                    validator: this.validateOrgID
                                }
                            ],initialValue:this.state.companyInfo.id
                    })(
                            <Input disabled={this.props.location.state} placeholder={'英文或数字'}/>
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label={"企业名称"}
                    >
                        {getFieldDecorator('name',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.companyInfo.name
                    })(
                            <Input />
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label={"公司地址"}
                    >
                        {getFieldDecorator('companyAddress',{
                            rules: [
                                {
                                    required: this.state.type==="1",
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.companyInfo.companyAddress
                    })(
                            <Input />
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label={"公司电话"}
                    >
                        {getFieldDecorator('companyPhone',{
                            rules: [
                                {
                                    required: this.state.type==="1",
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.companyInfo.companyPhone
                    })(
                            <Input />
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label={"负责人姓名"}
                    >
                        {getFieldDecorator('chiefName',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.companyInfo.chiefName
                    })(
                            <Input />
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label={"负责人电话"}
                    >
                        {getFieldDecorator('chiefPhone',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.companyInfo.chiefPhone
                    })(
                            <Input />
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label={"企业类型"}
                    >
                        {getFieldDecorator('saasType',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.companyInfo.saasType
                    })(
                            <SCSelect url={()=>{return axios.post('/api/corp/type/qry')}} />
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label={"SAAS权限"}
                    >
                        {getFieldDecorator('role',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.companyInfo.role
                    })(
                            <SCSelect url={()=>{return axios.post('/api/corp/role/qry')}} />
                        )}
                    </FormItem>
                    <FormItem
                    {...formItemLayout}
                    label={"用户数上限"}
                    >
                        {getFieldDecorator('userCntLimit',{
                            rules: [
                                {
                                    required: true,
                                    message: '该项为必填项'
                                }
                            ],initialValue:this.state.companyInfo.userCntLimit
                        })(
                            <InputNumber />
                        )}
                    </FormItem>
                    <FormItem 
                    {...formItemLayout}
                    label={"星卡"}
                    >
                    {getFieldDecorator('card', {
                        valuePropName: 'checked',
                        initialValue:this.state.companyInfo.card
                    })(
                        <Checkbox><a>支持刷卡充电</a></Checkbox>
                    )}
                    </FormItem>
                    <FormItem 
                    {...formItemLayout}
                    label={"短信"}
                    >
                    {getFieldDecorator('sendSms', {
                        valuePropName: 'checked',
                        initialValue:this.state.companyInfo.sendSms
                    })(
                        <Checkbox><a>发送短信通知</a></Checkbox>
                    )}
                    </FormItem>
                
                    {/* 详情==========  */}
                    <FormItem
                    {...tailFormItemLayout}
                    >
                        <Button className={"ant-btn-84"} type={'primary'} onClick={this.handleSubmit}>下一步</Button>
                    </FormItem>
                </form>
            </div>
        )
    }
}

AddCompany = createForm()(AddCompany);
export default injectIntl(AddCompany)
